/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.blue.xml;

import java.util.*;
import net.n3.nanoxml.IXMLElement;

/**
 *
 * @author kelvio.matias
 */
public class XMLElementImpl implements XMLElement {
    
    /**
     *
     */
    private IXMLElement element;

    /**
     * Create an WindowsXMLElement that wraps a IXMLElement of NanoXML API.
     */
    public XMLElementImpl(IXMLElement element) {
        if (element == null) {
            throw new IllegalArgumentException("The IXMLElement cannot be null.");
        }
        this.element = element;
    }

    /**
     * {@inheritDoc }
     *
     * @return
     */
    @Override
    public String getName() {
        return element.getName();
    }

    /**
     * {@inheritDoc }
     *
     * @return
     */
    @Override
    public String getValue() {
        return this.element.getContent();
    }

    /**
     * {@inheritDoc }
     *
     * @param name
     * @return
     */
    @Override
    public String getAttribute(String name) {
        return this.element.getAttribute(name, null);
    }

    /**
     * {@inheritDoc }
     *
     * @return
     */
    @Override
    public Properties getAttributes() {
        return this.element.getAttributes();
    }

    /**
     * {@inheritDoc }
     *
     * @param index
     * @return
     */
    @Override
    public XMLElement getChild(int index) {
        return new XMLElementImpl(this.element.getChildAtIndex(index));
    }

    /**
     * {@inheritDoc }
     *
     * @return
     */
    @Override
    public XMLElement getFirstChild() {
        return new XMLElementImpl(this.element.getChildAtIndex(0));
    }

    /**
     * {@inheritDoc }
     *
     * @return
     */
    @Override
    public XMLElement getLastChild() {
        return new XMLElementImpl(this.element.getChildAtIndex(this.element.getChildrenCount() - 1));
    }

    /**
     * {@inheritDoc }
     *
     * @return
     */
    @Override
    public Enumeration<XMLElement> enumerateChildren() {
        Enumeration<IXMLElement> childs = this.element.enumerateChildren();
        List<XMLElement> WindowsChilds = new LinkedList<XMLElement>();
        while (childs.hasMoreElements()) {
            WindowsChilds.add(new XMLElementImpl(childs.nextElement()));
        }
        return Collections.enumeration(WindowsChilds);
    }

    /**
     * {@inheritDoc }
     *
     * @return
     */
    @Override
    public Enumeration<String> enumerateAttributeNames() {
        Enumeration<String> attributeNames = this.element.enumerateAttributeNames();
        List<String> WindowsAttributeNames = new LinkedList<String>();
        while (attributeNames.hasMoreElements()) {
            WindowsAttributeNames.add(attributeNames.nextElement());
        }
        return Collections.enumeration(WindowsAttributeNames);
    }

    @Override
    public Integer getAttributeAsInteger(String name) {
        return new Integer(this.element.getAttribute(name, null));
    }

    @Override
    public Double getAttributeAsDouble(String name) {
        return new Double(this.element.getAttribute(name, null));
    }

    @Override
    public Float getAttributeAsFloat(String name) {
        return new Float(this.element.getAttribute(name, null));
    }

    @Override
    public Boolean getAttributeAsBoolean(String name) {
        return Boolean.getBoolean(this.element.getAttribute(name, null));
    }

    @Override
    public Short getAttributeAsShort(String name) {
        return new Short(this.element.getAttribute(name, null));
    }

    @Override
    public Byte getAttributeAsByte(String name) {
        return new Byte(this.element.getAttribute(name, null));
    }
}
